﻿using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Xarial.XCad.Base.Attributes;
using Xarial.XCad.UI.Commands;

namespace Test
{
    [ComVisible(true)]
    [Title("这个是插件你好")]
    public class Addin : Xarial.XCad.SolidWorks.SwAddInEx
    {
        /// <summary>
        ///  The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;
        ISldWorks iSwApp = null;
        public ISldWorks SwApp
        {
            get { return iSwApp; }
        }
        ModelDoc2 swDoc = null;
        PartDoc swPart = null;
        DrawingDoc swDrawing = null;
        AssemblyDoc swAssembly = null;
        bool boolstatus = false;
        int longStatus = 0;
        int longWarnings = 0;
        public override void OnConnect()
        {
            Application.ShowMessageBox("这里的是我的 Start!!!");
            CommandManager.AddCommandGroup<SwCommand>().CommandClick += Addin_CommandClick;

        }

        private void Addin_CommandClick(SwCommand spec)
        {
            Application.ShowMessageBox("这里的是我的 Addin_CommandClick!!!");
            //make sure we have a part open
            string partTemplate = iSwApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateAssembly);//swDefaultTemplateDrawing
            if ((partTemplate != null) && (partTemplate != ""))
            {
                IModelDoc2 modDoc = (IModelDoc2)iSwApp.NewDocument(partTemplate, (int)swDwgPaperSizes_e.swDwgPaperA2size, 0.0, 0.0);

                modDoc?.InsertSketch2(true);
                modDoc?.SketchRectangle(0, 0, 0, .1, .1, .1, false);
                //Extrude the sketch
                IFeatureManager featMan = modDoc?.FeatureManager;
                featMan?.FeatureExtrusion(true,
                    false, false,
                    (int)swEndConditions_e.swEndCondBlind, (int)swEndConditions_e.swEndCondBlind,
                    0.1, 0.0,
                    false, false,
                    false, false,
                    0.0, 0.0,
                    false, false,
                    false, false,
                    true,
                    false, false);
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("There is no part template available. Please check your options and make sure there is a part template selected, or select a new part template.");
            }
        }

        public override void OnDisconnect()
        {

        }
    }
}
